<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="noticeQuery.name" placeholder="用户名" />
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="noticeQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="noticeQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button type="danger" @click="addInternNotice()">新增企业公告</el-button>
      <el-button type="danger" @click="addTeachNotice()">新增教师公告</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column label="ID号" width="70" align="center">
        <template slot-scope="scope">
          <!-- {{ (page - 1) * limit + scope.$index + 1 }} -->
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column prop="uname" label="发布人" width="200">
        <template slot-scope="scope">
          {{ scope.row.uname }}
          <el-tag v-if="scope.row.utype == 2" type="info">教师</el-tag>
          <el-tag v-else-if="scope.row.utype === 3" type="warning">企业</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="noticeContent" label="通知内容" width="200">
      </el-table-column>

      <el-table-column prop="status" label="状态" :label-width="formLabelWidth">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">审核中</el-tag>
          <el-tag v-else-if="scope.row.status == 2" type="info">上线</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="warning">下架</el-tag>
          <!-- {{ scope.row.type==0?'管理员':'首席讲师' }} -->
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="通知时间" width="400" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" plain @click="toIssue(scope.row.id)"
            >发布</el-button
          >

          <el-button
            type="warning"
            size="mini"
            plain
            @click="toUndercarriage(scope.row.id)"
            >下架</el-button
          >

          <el-button type="warning" size="mini" plain @click="updateNotice(scope.row.id)"
            >修改通知</el-button
          >

          <el-button
            type="success"
            size="mini"
            plain
            @click="deleteNoticeById(scope.row.id)"
            >删除通知</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改公告通知" :visible.sync="updateNoticeVisible">
      <el-form :model="noticeObj">
        <el-form-item label="发布人" :label-width="formLabelWidth">
          <el-input v-model="noticeObj.uname" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="通知内容" :label-width="formLabelWidth">
          <el-input
            v-model="noticeObj.noticeContent"
            type="textarea"
            maxlength="200"
            show-word-limit
            rows="6"
          ></el-input>
        </el-form-item>
        <el-form-item label="通知时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="noticeObj.updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateNoticeVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateNoticeById(noticeObj)">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增企业公告通知" :visible.sync="addInternNoticeVisible">
      <el-form :model="noticeObj">
        <el-form-item label="发布人" :label-width="formLabelWidth">
          <el-autocomplete
            v-model="noticeObj.uname"
            :fetch-suggestions="querySearchAsyncForIntern"
            placeholder="请点击"
            @select="internHandleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="通知内容" :label-width="formLabelWidth">
          <el-input
            placeholder="请输入内容"
            v-model="noticeObj.noticeContent"
            type="textarea"
            maxlength="200"
            show-word-limit
            rows="6"
          ></el-input>
        </el-form-item>

        <el-form-item label="通知时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="noticeObj.updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateNoticeVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateNoticeById(noticeObj)">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增教师公告通知" :visible.sync="addTeacherNoticeVisible">
      <el-form :model="teacherNotice">
        <el-form-item label="发布人" :label-width="formLabelWidth">
          <el-autocomplete
            v-model="teacherNotice.uname"
            :fetch-suggestions="querySearchAsyncForTeacher"
            placeholder="请点击"
            @select="teacherHandleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="通知内容" :label-width="formLabelWidth">
          <el-input
            placeholder="请输入内容"
            v-model="teacherNotice.noticeContent"
            type="textarea"
            maxlength="200"
            show-word-limit
            rows="6"
          ></el-input>
        </el-form-item>
        <el-form-item label="通知时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="teacherNotice.updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addTeacherNoticeVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateNoticeById(teacherNotice)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>
import noticeApi from "@/api/notice";
import internApi from "@/api/internship";
import teachApi from "@/api/teach";

export default {
  data() {
    return {
      list: null,
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 0, //总记录数
      noticeQuery: {}, //条件封装对象
      //通知
      noticeObj: {
        uid: "",
        uname: "",
        noticeContent: "",
        createTime: "",
        updateTime: "",
      },
      teacherNotice: {
        uid: "",
        uname: "",
        noticeContent: "",
        createTime: "",
        updateTime: "",
      },
      updateNoticeVisible: false,
      addInternNoticeVisible: false,
      addTeacherNoticeVisible: false,
      formLabelWidth: "120px",
      internRestaurants: [],
      teacherRestaurants: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    getList() {
      noticeApi
        .getNoticeList(this.page, this.limit, this.noticeQuery)
        .then((response) => {
          this.list = response.data.notices;
        });
    },
    //加载企业数据
    loadAllIntern() {
      internApi.getSampleInternshipList().then((resp) => {
        this.internRestaurants = resp.data.list;
      });
    },
    //加载教师数据
    loadAllTeacher() {
      teachApi.getSampleTeacherList().then((resp) => {
        this.teacherRestaurants = resp.data.list;
        console.log(this.teacherRestaurants);
      });
    },
    //上线
    toIssue(id) {
      noticeApi.toIssue(id).then((resp) => {
        if (resp.data.status) {
          this.$message({
            message: "上线成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
    //下架
    toUndercarriage(id) {
      noticeApi.toUndercarriage(id).then((resp) => {
        if (resp.data.status) {
          this.$message({
            message: "下架成功",
            type: "success",
          });
          this.getList();
        }
      });
    },

    querySearchAsyncForIntern(queryString, cb) {
      let restaurants = this.internRestaurants;
      let results = queryString
        ? restaurants.filter(this.createStateFilter(queryString))
        : restaurants;
      clearTimeout(this.timeout);
      cb(results);
    },
    querySearchAsyncForTeacher(queryString, cb) {
      let restaurants = this.teacherRestaurants;
      let results = queryString
        ? restaurants.filter(this.createStateFilter(queryString))
        : restaurants;
      clearTimeout(this.timeout);
      cb(results);
    },
    createStateFilter(queryString) {
      return (state) => {
        return state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    //选中触发
    internHandleSelect(item) {
      this.noticeObj.uid = item.id;
      this.noticeObj.uname = item.value;
    },
    teacherHandleSelect(item) {
      this.teacherNotice.uid = item.id;
      this.teacherNotice.uname = item.value;
    },

    //打开企业添加通知窗口
    addInternNotice() {
      this.loadAllIntern();
      this.noticeObj = {};
      this.addInternNoticeVisible = true;
    },
    //打开教师添加通知窗口
    addTeachNotice() {
      this.loadAllTeacher();
      this.teacherNotice = {};
      this.addTeacherNoticeVisible = true;
    },

    //打开修改评论
    updateNotice(id) {
      //将id赋值都vue对象参数内
      this.noticeId = id;
      this.updateNoticeVisible = true;

      //获取当前选中的评价行记录
      noticeApi.getNoticeInfo(id).then((response) => {
        this.noticeObj = response.data.notice;
        console.log(this.noticeObj);
      });
    },

    //修改评论
    updateNoticeById(notice) {
      noticeApi.saveOrUpdate(notice).then((resp) => {
        this.$message({
          type: "success",
          message: "更新成功!",
        });
        this.getList();

        this.addInternNoticeVisible = false;
        this.addTeacherNoticeVisible = false;
      });
    },

    //删除评价
    deleteNoticeById(id) {
      this.$confirm("此操作将删除评价, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        noticeApi.deleteNotice(id).then((response) => {
          //提示信息
          this.$message({
            type: "success",
            message: "移除成功!",
          });
          //再次渲染
          this.getList();
        });
      });
    },
  },
};
</script>

<style></style>
